package com.hexinfo.dmpro.script.publish.service.impl;

import cn.hutool.core.util.StrUtil;
import com.hexinfo.dmpro.common.dao.ClusterSparingConfMapper;
import com.hexinfo.dmpro.script.config.model.ConfigPropertyJdbcconfig;
import com.hexinfo.dmpro.script.config.model.ConfigPropertyParam;
import com.hexinfo.dmpro.script.config.model.ConfigPropertyPublishuser;
import com.hexinfo.dmpro.script.config.model.ConfigPropertyServer;
import com.hexinfo.dmpro.script.config.service.ConfigPropertyJdbcconfigService;
import com.hexinfo.dmpro.script.config.service.ConfigPropertyParamService;
import com.hexinfo.dmpro.script.config.service.ConfigPropertyPublishuserService;
import com.hexinfo.dmpro.script.config.service.ConfigPropertyServerService;
import com.hexinfo.dmpro.script.publish.service.ConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class ConfigServiceImpl implements ConfigService {

    private final ConfigPropertyPublishuserService configPropertyPublishuserService;

    private final ConfigPropertyServerService configPropertyServerService;

    private final ConfigPropertyJdbcconfigService configPropertyJdbcconfigService;

    private final ConfigPropertyParamService configPropertyParamService;

    private final ClusterSparingConfMapper clusterSparingConfMapper;

/**
 * @Method getParam
 * @Param clusterName
 * @Return java.util.Map<java.lang.String,java.lang.String>
 * @Description 获取param
 * @Author le.zhang
 * @Date 2024/3/4 13:16
 * @Version V1.0
 */
    @Override
    public Map<String,String> getParam(String clusterName){
        String clusterId = clusterSparingConfMapper.getId(clusterName);
        List<ConfigPropertyParam> paramList = configPropertyParamService.findAll();
        Map<String, String> paramsMap = new HashMap<>();
        for (ConfigPropertyParam param : paramList) {
            if (clusterId.equals(param.getClusterId())&&!("true".equals((param.getDisabled())))){
                paramsMap.put(param.getBeforeParam(), null != param.getEndParam() ? param.getEndParam() : "");
            }
        }
        return paramsMap;
    }

/**
 * @Method getPublishUser
 * @Param clusterName
 * @Param username
 * @Return java.util.Map<java.lang.String,java.lang.String>
 * @Description 获取publish-user
 * @Author le.zhang
 * @Date 2024/3/4 13:16
 * @Version V1.0
 */
    @Override
    public Map<String,String> getPublishUser(String clusterName,String username){
        String clusterId = clusterSparingConfMapper.getId(clusterName);
        ConfigPropertyServer configPropertyServer = new ConfigPropertyServer();
        configPropertyServer.setClusterId(clusterId);
        List<ConfigPropertyServer> configPropertyServerList = configPropertyServerService.queryListNoPage(configPropertyServer);
        List<ConfigPropertyPublishuser> publishUserList = configPropertyPublishuserService.findAll();
        Map<String,String> userInfoMap = new HashMap<>();
        for (ConfigPropertyPublishuser c : publishUserList) {
            if (username.equals(c.getUsername())&&configPropertyServerList.get(0).getId().equals(c.getServerId())) {
                userInfoMap.put("username",c.getUsername());
                userInfoMap.put("password",c.getPassword());
            }
        }
        return userInfoMap;
    }

    /**
     * @Method getPublishUser
     * @Param
     * @param serverId
     * @param clusterName
     * @param username
     * @Return java.util.Map<java.lang.String,java.lang.String>
     * @Description 获取用户信息
     * @Author Wang zhihao
     * @Date 2024/3/26 19:03
     * @Version V1.0
     */
    @Override
    public Map<String,String> getPublishUser(String serverId, String clusterName,String username){
        try {
            ConfigPropertyPublishuser configPropertyPublishuser = configPropertyPublishuserService.findUserByInfo(new ConfigPropertyPublishuser().setServerId(serverId).setUsername(username));
            Map<String,String> userInfoMap = new HashMap<>();
            userInfoMap.put("username", configPropertyPublishuser.getUsername());
            userInfoMap.put("password", configPropertyPublishuser.getPassword());
            return userInfoMap;
        } catch (Exception e) {
            log.error("获取用户信息失败" + e.getMessage(), e);
            throw new RuntimeException("获取用户信息失败" + e.getMessage(), e);
        }
    }

/**
 * @Method getJdbc
 * @Param jdbcName
 * @Param clusterName
 * @Return java.util.Map<java.lang.String,java.lang.String>
 * @Description 获取jdbc配置信息
 * @Author le.zhang
 * @Date 2024/3/4 13:17
 * @Version V1.0
 */
    @Override
    public Map<String,String> getJdbc(String jdbcName,String clusterName){
        String clusterId = clusterSparingConfMapper.getId(clusterName);
        List<ConfigPropertyJdbcconfig> jdbcList = configPropertyJdbcconfigService.findAll();
        Map<String, String> impalaConnInfo = new HashMap<>();
        for (ConfigPropertyJdbcconfig c :
                jdbcList) {
            if (!("true".equals((c.getDisabled())))&&jdbcName.equals(c.getJdbcName()) && clusterId.equals(c.getClusterId())) {
                impalaConnInfo.put("db-type", c.getDbType());
                impalaConnInfo.put("driverClassName", c.getDriverclassname());
                impalaConnInfo.put("url", c.getUrl());
                impalaConnInfo.put("username", c.getUsername());
                impalaConnInfo.put("password", c.getPassword());
            }
        }
        return impalaConnInfo;
    }

/**
 * @Method getScp
 * @Param clusterName
 * @Return java.util.Map<java.lang.String,java.lang.String>
 * @Description 获取scp
 * @Author le.zhang
 * @Date 2024/3/4 13:17
 * @Version V1.0
 */
    @Override
    public Map<String,String> getScp(String clusterName){
        String clusterId = clusterSparingConfMapper.getId(clusterName);
        List<ConfigPropertyServer> serverList = configPropertyServerService.findAll();
        Map<String,String> scpConnInfo = new HashMap<>();
        for (ConfigPropertyServer c :
                serverList) {
            if (clusterId.equals(c.getClusterId())){
                scpConnInfo.put("host",c.getHost());
                scpConnInfo.put("port",c.getPort());
                scpConnInfo.put("mirror_host",c.getMirrorHost());
                scpConnInfo.put("mirror_port",c.getMirrorPort());
            }
        }
        return scpConnInfo;
    }


}
